<?php

class Nguoidung extends MY_Admin
{
	// Protected or private properties
	protected $_template;
	
	// Constructor
	public function __construct()
	{
		parent::__construct();

		// Check if the logged user is an administrator
		//$this->access_library->check_access();
		// Load needed models, libraries, helpers and language files
		$this->load->module_model('admin', 'nguoidung_model', 'nguoidung');
		$this->load->module_language('admin', 'general');
		$this->load->module_language('admin', 'nguoidung');
		$this->user = $this->access->get_user();
		$this->load->helper('url'); 
		//You should autoload this one ;)
		//$this->access->restrict('Admin.Hocvien.Index');
		//$this->access->restrict();
	}

	// Public methods
	/*
	 * ADDED BY MANHVT
	 */
	public function addgroup()
	{
		$this->access->restrict('Admin.Nguoidung.Add');
		$data['ItemID'] = 'User';
		$data['title'] = 'Thêm mới nhóm người dùng';
		$data['description'] = 'Thêm mới nhóm người dùng';
		$data['role'] 		= $this->nguoidung->get_role();
		$this->load->helper('form');
		$userid = $this->user->id;
		$lang_working = load_language_working();
		
		//here is validation form
		$this->form_validation->set_rules('username', 'lang:th_username', 'required|max_length[50]|xss_clean|callback_check_username');
		$this->form_validation->set_rules('password', 'lang:th_password', 'required');
		$this->form_validation->set_rules('email', 'lang:th_email', 'required|xss_clean|trim|valid_email');
		//$this->form_validation->set_rules('role','lang:th_role','required|max_length[255]');		
		$this->form_validation->set_rules('ngaysinh','Ngày sinh','required|callback_date_check');
		$this->form_validation->set_error_delimiters('', '<br />');

		if ($this->form_validation->run() == TRUE)
		{		
			$this->nguoidung->add_nguoidung();
			$this->session->set_flashdata('message', lang('successfully_created'));	
			redirect('admin/nguoidung/groups', 'refresh');
		}	
		
		$this->_template['page']	= 'nguoidung/addgroup';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	/*
	 * Liệt kê danh sách nhóm người dùng trong hệ thống
	 */
	public function groups()
	{
		$this->access->restrict('Admin.Nguoidung.Index');
		
		$data['ItemID'] = 'User';
		$data['title'] = 'Danh sách nhóm người dùng';
		$data['description'] = 'Quản lý danh sách nhóm người dùng';
		
		
		$this->load->library('basic_library');
		$this->load->library('pagination');
		$this->load->helper('url');
		$config = array();
		$config['base_url'] 	= base_url().'admin/nguoidung/';
		$config['total_rows'] 	= $this->nguoidung->count_all_nguoidung();
		$config['per_page'] 	= 40;
		$config['uri_segment']	= 3;
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(3))? $this->uri->segment(3):0;
		
		$data['users'] 	= $this->nguoidung->get_role();
		//$data['lops'] 		= $this->nguoidung->get_tenlop();
		$data['links'] 		= $this->pagination->create_links();		
		
		$this->_template['page']	= 'nguoidung/listgroup';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	/*
	 * Display danh sách quyền cho từng module
	 * Cập nhật các quyền cho module
	 */
	public function displaypermission($role_id)
	{
		$this->access->restrict('Admin.Nguoidung.Index');
		
		$data['ItemID'] = 'User';
		$data['title'] = 'Danh sách nhóm người dùng';
		$data['description'] = 'Quản lý danh sách nhóm người dùng';
		
		
		$this->load->library('basic_library');
		$this->load->library('pagination');
		$this->load->helper('url');
		$config = array();
		$config['base_url'] 	= base_url().'admin/nguoidung/';
		$config['total_rows'] 	= $this->nguoidung->count_all_nguoidung();
		$config['per_page'] 	= 40;
		$config['uri_segment']	= 3;
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(3))? $this->uri->segment(3):0;
		
		$roles 	= $this->nguoidung->get_permission_by_role_id($role_id);
		$modules 	= $this->nguoidung->get_modules();
		$arraytmp = array();
		//var_dump
		foreach($roles as $role)
		{
			foreach($modules as $key=>$module)
			{
				
				$flag = false;
				if($role['name'] == $module['name'])
				{
					$checkbox = array(
				    'name'        => 'action_old[]',
				    'id'          => $module['name'],
				    'value'       => $module['permission_id'],
				    'checked'     => TRUE,
				    'style'       => 'margin:10px',
				    );
				    $module['checkbox'] = $checkbox;							
					$flag = true;
				}				
				$modules[$key] = $module;
				if($flag)
				{
					break;
				}
			}
		}
		foreach($modules as $key=>$module)
		{
			$checkbox = array(
				    'name'        => 'action_new[]',
				    'id'          => $module['name'],
				    'value'       => $module['permission_id'],
				    'checked'     => false,
				    'style'       => 'margin:10px',
				    );
			if(!isset($module['checkbox']))
			{
				$module['checkbox'] = $checkbox;
				$modules[$key] = $module;
			}
		}
		$data['users']= $modules;
		$data['role_id'] = $role_id;
		//$data['lops'] 		= $this->nguoidung->get_tenlop();
		$data['links'] 		= $this->pagination->create_links();		
		
		$this->_template['page']	= 'nguoidung/listpermission';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	public function changepermission()
	{
		$role_id = $this->input->post('role_id');
		$newpermission = $this->input->post('action_new');
		$oldpermission = $this->input->post('action_old');
		$rolepermission = $this->nguoidung->get_permission_by_role_id($role_id);
		if($newpermission != false)
		{
			//create new permission for the role			
			foreach($newpermission as $permission)
			{
				$this->nguoidung->add_role($role_id,$permission);
			}
		}
		if($oldpermission != false)
		{
			if(count($oldpermission)!= count($rolepermission))
			{
				$this->nguoidung->delete_roles($role_id,$oldpermission);
			}
		}
		redirect("admin/nguoidung/displaypermission/$role_id", 'refresh');
	}
	public function index() 
	{
		//echo $this->user->role_id;
		$this->access->restrict('Admin.Nguoidung.Index');
		
		$data['ItemID'] = 'User';
		$data['title'] = 'Danh sách Người dùng';
		$data['description'] = 'Quản lý danh sách Người dùng';
		
		
		$this->load->library('basic_library');
		$this->load->library('pagination');
		$this->load->helper('url');
		$config = array();
		$config['base_url'] 	= base_url().'admin/nguoidung/';
		$config['total_rows'] 	= $this->nguoidung->count_all_nguoidung();
		$config['per_page'] 	= 40;
		$config['uri_segment']	= 3;
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(3))? $this->uri->segment(3):0;
		
		$data['users'] 	= $this->nguoidung->get_all_nguoidung($config['per_page'], $page);
		//$data['lops'] 		= $this->nguoidung->get_tenlop();
		$data['links'] 		= $this->pagination->create_links();		
		
		$this->_template['page']	= 'nguoidung/list';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	public function donghocphi()
	{
		if($this->user->id)
		{
			$result = $this->hocvien->add_hocphi();
			$row['result'] = $result;			
			echo json_encode($row);	
		}
		else
		{
			$row['result'] = 0;
			echo json_encode($row);
		}
	}
	
	public function search()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Tìm kiếm thông tin';
		$data['description'] = 'Tìm kiếm thông tin';
		$data['search_keyword'] = trim($this->input->post('keyword', TRUE));
		if($data['search_keyword']!=='')
		{
			$data['result'] = $this->nguoidung->search_data($data['search_keyword']);
			
				$this->_template['page']	= 'nguoidung/result';
				$this->system_library->load($this->_template['page'], $data, TRUE);
			
			
		}
		else
		{
			redirect('admin/hocvien', 'refresh');
		}
	}
	
	public function add()
	{
		$this->access->restrict('Admin.Nguoidung.Add');
		$data['ItemID'] = 'User';
		$data['title'] = 'Thêm mới Người dùng';
		$data['description'] = 'Thêm mới Người dùng';
		$data['role'] 		= $this->nguoidung->get_role();
		$this->load->helper('form');
		$userid = $this->user->id;
		$lang_working = load_language_working();
		
		//here is validation form
		$this->form_validation->set_rules('username', 'lang:th_username', 'required|max_length[50]|xss_clean|callback_check_username');
		$this->form_validation->set_rules('password', 'lang:th_password', 'required');
		$this->form_validation->set_rules('email', 'lang:th_email', 'required|xss_clean|trim|valid_email');
		//$this->form_validation->set_rules('role','lang:th_role','required|max_length[255]');		
		$this->form_validation->set_rules('ngaysinh','Ngày sinh','required|callback_date_check');
		$this->form_validation->set_error_delimiters('', '<br />');

		if ($this->form_validation->run() == TRUE)
		{		
			$this->nguoidung->add_nguoidung();
			$this->session->set_flashdata('message', lang('successfully_created'));	
			redirect('admin/nguoidung/', 'refresh');
		}	
		
		$this->_template['page']	= 'nguoidung/add';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	public function edit($id)
	{
		$this->access->restrict('Admin.Nguoidung.Edit');
		//metadata
		$data['ItemID']		 = 'User';
		$data['title']		 = "Sửa thông tin người dùng"; 
		$data['description'] = "Sửa thông tin người dùng"; 
		$data['info'] 		= $this->nguoidung->get_nguoidung_info($id);
		$data['role'] 		= $this->nguoidung->get_role();
				
		if($data['info']->username==$this->input->post('username'))
		{
			$this->form_validation->set_rules('username', 'lang:form_username', 'required|max_length[50]|xss_clean');
		}
		else
		{
			$this->form_validation->set_rules('username', 'lang:form_username', 'required|max_length[50]|xss_clean|callback_check_username');
		}
		$this->form_validation->set_rules('fullname', 'lang:th_fullname', 'required');
		
		if($this->input->post('password')=='')
		{
			$this->form_validation->set_rules('password','lang:th_password','');
			$this->form_validation->set_rules('repassword','lang:th_repassword','');
		}
		else
		{
			$this->form_validation->set_rules('password','lang:th_password','required|trim');
			$this->form_validation->set_rules('repassword','lang:th_repassword','required|matches[password]|trim');
		}
		
		$this->form_validation->set_rules('email','lang:th_thuongtru','required|max_length[255]|valid_email');
		$this->form_validation->set_rules('ngaysinh','Ngày sinh','required|callback_date_check');
					
		$this->form_validation->set_error_delimiters('', '<br />');
		
		if($this->form_validation->run() == TRUE)
		{
			$this->nguoidung->edit_nguoidung($id);
			$this->session->set_flashdata('message', lang('successfully_edited'));	
			redirect('admin/nguoidung/', 'refresh');	
		}	
		$this->_template['page']	= 'nguoidung/edit';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	function date_check($date) 
	{
    	$ddmmyyy='(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)[0-9]{2}';
    	if(preg_match("/$ddmmyyy$/", $date))
    	{
      		return TRUE;
    	} else
    	{
     		$this->form_validation->set_message('date_check', 'Nhập định dạng ngày tháng dd/mm/yyyy');
     	return FALSE;
    	}
	}  
	
	public function check_mahv()
	{
        if($this->hocvien->check_mahv())
        {
            $this->form_validation->set_message('check_mahv','MAHV đã tồn tại, vui lòng chọn lại');
            return false;
        }
        else
        {
            
            return true;
        }
    }
	public function check_username()
	{
        if($this->nguoidung->check_username())
        {
            $this->form_validation->set_message('check_username','CMND đã tồn tại, vui lòng chọn lại');
            return false;
        }
        else
        {
            
            return true;
        }
    }
	
	public function delete($id)
	{
		$this->access->restrict('Admin.Nguoidung.Delete');
		if($id == 1)
		{
			$this->session->set_flashdata('message', 'Không thể xóa được Supper User này!');
		}
		else
		{
			$this->hocvien->delete_hocvien($id);
			$this->session->set_flashdata('message', lang('successfully_deleted'));
		}
		
		redirect('admin/nguoidung', 'refresh');
	}
	
	public function enable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->nguoidung->change_status_nguoidung($id,1);
		redirect('admin/nguoidung', 'refresh');
		
	}
	public function disable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->nguoidung->change_status_nguoidung($id,0);
		redirect('admin/nguoidung', 'refresh');
	}
}

/* End of file jobs.php */